﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.IO;

namespace Acms.AcmsControls.ContentControls.Admin
{
    /// <summary>
    /// This calls is used to help develop custom admin pages. Create a default.aspx with a placholder in it and inherit from this class. It will display the usercontrols allowing developer to click on it and set up correct quesry string
    /// </summary>
    public abstract class CustomAdminDevLoader : Page
    {
        protected abstract PlaceHolder Placeholder { get; }

        protected override void CreateChildControls()
        {
            BaseCustomAdminControl uc = null;
            if (!string.IsNullOrEmpty(Request.QueryString["Path"]))
            {
                if (!string.IsNullOrEmpty(breadcrumbs))
                {
                    Placeholder.Controls.Add(new LiteralControl(breadcrumbs));
                }
                uc = (BaseCustomAdminControl)Page.LoadControl(Request.QueryString["Path"]);
                uc.AcmsContext = this.Context.Items["AcmsContext"] as AcmsContext;
                Placeholder.Controls.Add(uc);
            }

            base.CreateChildControls();
        }

        private string breadcrumbs;
        protected override object SaveViewState()
        {
            ViewState["breadcrumbs"] = breadcrumbs;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            breadcrumbs = ViewState["breadcrumbs"] as string;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                EnsureChildControls();
                if (Placeholder.Controls.Count > 0 && Placeholder.Controls[0] is BaseCustomAdminControl)
                {
                    var items = new Dictionary<string, string>();
                    ((BaseCustomAdminControl)Placeholder.Controls[0]).GetExtaBreadCrumbs(items);

                    StringBuilder sb = new StringBuilder();
                    sb.Append("<ul>");
                    sb.AppendFormat("<li><a href=\"{0}\">Custom Admin</a></li>", ResolveUrl("~"));
                    sb.AppendFormat("<li>{0}</li>", Request.QueryString["Name"]);
                    foreach (var item in items)
                    {
                        sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", ResolveUrl(item.Value), item.Key);
                    }
                    sb.Append("</ul>");
                    breadcrumbs = sb.ToString();
                    Placeholder.Controls.AddAt(0, new LiteralControl(breadcrumbs));
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (Placeholder != null && Placeholder.Controls.Count == 0)
            {
                var files = Directory.GetFiles(Server.MapPath("~"), "*.ascx").Select(x => new FileInfo(x)).Select(x => new {Name = x.Name.Substring(0, x.Name.IndexOf(".ascx")), Path = string.Concat("~/", x.Name)});
                var ul = new HtmlGenericControl("ul");
                Placeholder.Controls.Add(ul);
                foreach (var file in files)
                {
                    ul.Controls.Add(new LiteralControl(string.Format("<li><a href=\"{0}?Name={1}&Path={2}\">{1}</a></li>", ResolveUrl("~/Default.aspx"), file.Name, System.Web.HttpUtility.UrlEncode(file.Path))));
                }
            }
            base.OnPreRender(e);
        }
    }
}
